@@ -0,0 +1,80 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ class BoxcarAgent < Agent |
|
| 3 |
+ |
|
| 4 |
+ cannot_be_scheduled! |
|
| 5 |
+ cannot_create_events! |
|
| 6 |
+ |
|
| 7 |
+ API_URL = 'https://new.boxcar.io/api/notifications' |
|
| 8 |
+ |
|
| 9 |
+ description <<-MD |
|
| 10 |
+ The Boxcar agent sends push notifications to iPhone. |
|
| 11 |
+ |
|
| 12 |
+ To be able to use the Boxcar end-user API, you need your `Access Token`. |
|
| 13 |
+ The access token is available on general "Settings" screen of Boxcar iOS |
|
| 14 |
+ app or from Boxcar Web Inbox settings page. |
|
| 15 |
+ |
|
| 16 |
+ Please provide the access token as `user_credentails` in `options` or create a |
|
| 17 |
+ `boxcar_api_key` credential. The value in `user_credentials` will be considered if |
|
| 18 |
+ specified in both places. |
|
| 19 |
+ |
|
| 20 |
+ Options: |
|
| 21 |
+ |
|
| 22 |
+ * `user_credentials` - Boxcar access token. |
|
| 23 |
+ * `title` - Title of the message. |
|
| 24 |
+ * `body` - Body of the message. |
|
| 25 |
+ * `source_name` - Name of the source of the message. Set to `Huginn` by default. |
|
| 26 |
+ * `icon_url` - URL to the icon. |
|
| 27 |
+ * `sound` - Sound to be played for the notification. Set to 'bird-1' by default. |
|
| 28 |
+ MD |
|
| 29 |
+ |
|
| 30 |
+ def default_options |
|
| 31 |
+ {
|
|
| 32 |
+ 'user_credentials' => '', |
|
| 33 |
+ 'title' => "{{title}}",
|
|
| 34 |
+ 'body' => "{{body}}",
|
|
| 35 |
+ 'source_name' => "Huginn", |
|
| 36 |
+ 'icon_url' => "", |
|
| 37 |
+ 'sound' => "bird-1" |
|
| 38 |
+ } |
|
| 39 |
+ end |
|
| 40 |
+ |
|
| 41 |
+ def working? |
|
| 42 |
+ received_event_without_error? |
|
| 43 |
+ end |
|
| 44 |
+ |
|
| 45 |
+ def strip(string) |
|
| 46 |
+ puts string |
|
| 47 |
+ string.strip! || string |
|
| 48 |
+ end |
|
| 49 |
+ |
|
| 50 |
+ def validate_options |
|
| 51 |
+ errors.add(:base, "you need to specify a boxcar api key") if options['user_credentials'].blank? |
|
| 52 |
+ end |
|
| 53 |
+ |
|
| 54 |
+ def receive(incoming_events) |
|
| 55 |
+ incoming_events.each do |event| |
|
| 56 |
+ payload_interpolated = interpolated(event) |
|
| 57 |
+ user_credentails = payload_interpolated['user_credentials'] || credential('boxcar_api_key')
|
|
| 58 |
+ post_params = {
|
|
| 59 |
+ 'user_credentials' => user_credentials, |
|
| 60 |
+ 'notification' => {
|
|
| 61 |
+ 'title' => strip(payload_interpolated['title']), |
|
| 62 |
+ 'long_message' => strip(payload_interpolated['body']), |
|
| 63 |
+ 'source_name' => payload_interpolated['source_name'], |
|
| 64 |
+ 'sound' => payload_interpolated['sound'], |
|
| 65 |
+ 'icon_url' => payload_interpolated['icon_url'] |
|
| 66 |
+ } |
|
| 67 |
+ } |
|
| 68 |
+ send_notification(post_params) |
|
| 69 |
+ end |
|
| 70 |
+ end |
|
| 71 |
+ |
|
| 72 |
+ def send_notification(post_params) |
|
| 73 |
+ response = HTTParty.post(API_URL, :query => post_params) |
|
| 74 |
+ raise StandardError, response['error']['message'] if response['error'].present? |
|
| 75 |
+ if response['Response'].present? and response['Response'] == "Not authorized" |
|
| 76 |
+ raise StandardError, response['Response'] |
|
| 77 |
+ end |
|
| 78 |
+ end |
|
| 79 |
+ end |
|
| 80 |
+end |